//
//  HMTabBar.swift
//  SecondWB
//
//  Created by 赖建伟 on 16/11/26.
//  Copyright © 2016年 Itheima. All rights reserved.
//

import UIKit

class HMTabBar: UITabBar {

    var closure:(()->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        addSubview(centerButton)
    }
    //MARK: - 布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width: CGFloat = kUIScreenWidth/5
        let height: CGFloat = bounds.size.height
        
        var index: NSInteger = 0
        
        for button in subviews {
            
//            print(button)
            if button.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                button.frame = CGRect.init(x: CGFloat(index)*width, y: 0, width: width, height: height)
                
                index == 1 ? (index = index + 2):(index = index + 1)
                
            }
            
        }
        
        centerButton.frame = CGRect.init(x: 2*width, y: 0, width: width, height: height)
        
    }
    
    
    //MARK: - 懒加载控件
    lazy var centerButton: UIButton = {
        
       let button =  UIButton(normalImage: "tabbar_compose_icon_add", backGroundImage: "tabbar_compose_button")
        
//        button.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
//        button.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
//        button.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), for: UIControlState.normal)
//        button.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
        
        button.addTarget(self, action: #selector(centerButtonClick), for: UIControlEvents.touchUpInside)
        
        
       return button
        
    }()
    
    func centerButtonClick() {
        closure?()
    }
    
}
